--[[

	Copyright (c) 2012-2020 baby-bus.com

	TODO:   TouchLayerName
	Author: DemonYao
	Date:   2020.10.12
 
	http://www.babybus.com/ 

]] 




--------------------------
-- 引入命名空间
--------------------------
-- local BaseLayer = import("app.common.layer.BaseLayer")




--[[
	类
--]]


local M = classLayerTouch("Main")




--------------------------
-- 构造函数
--------------------------

function M:ctor(params)
	-- [超类调用]
	M.super.ctor(self, params)
	-- [本类调用]
	self.suitconfig = {
		{
			"2010600a",
            "2080100e",
            "2080500b",
            "20208003",
            "20509005",
            "20703016",
		}, 
		{
			"2080b003",
            "20106010",
            "20208006",
            "20509007",
            "20703020",
            "20801015",
		}
	}
	self.replaceSuitParts = {}
	self.replaceSuselft   = {}
	self.replaceSuitIndex = 1
end

--------------------------
-- 渲染
--------------------------

function M:onRender()
	-- [超类调用]
	M.super.onRender(self)

	-- [本类调用]
	-- 加载节点
	self:initData()
	self:loadNodes()
end

-- 加载节点
function M:loadNodes()
	local armature = self:createNode("Node", {
		dragonBonesDataFile		= "you_xin/body/body_ske.json", 
		dragonBonesTexFile	    = "you_xin/body/body_tex.json",
		armatureName			= "body",
		animName				= "idle"
	}):p(450, 200):to(self):scale(0.25):rotation(180)
	-- }):p(450, 60):to(self):scale(0.25)--:rotation(180)
	self.armature = armature
	self:initFisrtSuit()
end

-- 加载渲染以外的其他操作
function M:onEnterTransitionFinish()
	--
end

function M:initData()
	self.resources = {}
	for i=1, T.count(self.suitconfig) do
		for k, partArmatureName in pairs(self.suitconfig[i]) do
			local path = "you_xin/suit" .. tostring(i).. "/"..partArmatureName
			local dragonBonesJSONPath = path .."_ske.json"
			local textureAtlasJSONPath = path.. "_tex.json"
			dragonBones.CCFactory:loadDragonBonesData(dragonBonesJSONPath)
			dragonBones.CCFactory:loadTextureAtlasData(textureAtlasJSONPath)
		end
	end
end

-- Init the first suit.
function M:initFisrtSuit()
	for k,v in pairs(self.suitconfig[1]) do
		local partArmatureData = dragonBones.CCFactory:getArmatureData(v);
		dragonBones.CCFactory:replaceSkin(self.armature.armatureDisplay:getArmature(), partArmatureData:getDefaultSkin());
	end
end

function M:randomReplaceSkin()
	-- This suit has been replaced, next suit.
	if #self.replaceSuitParts == 0 then
		self.replaceSuitIndex = self.replaceSuitIndex + 1
		print( #self.suitconfig )
		if self.replaceSuitIndex > #self.suitconfig then
			self.replaceSuitIndex = 1;
		end

		-- Refill the unset parits.
		for k,v in pairs(self.suitconfig[self.replaceSuitIndex]) do
			table.insert(self.replaceSuitParts, v)
		end
	end

	-- Random one part in this suit.
	local partIndex = math.floor(math.random(1, 100) / 100 * #self.replaceSuitParts) + 1
	local partArmatureName = self.replaceSuitParts[partIndex]
	local partArmatureData = dragonBones.CCFactory:getArmatureData(partArmatureName);
	--  Replace skin.
	dragonBones.CCFactory:replaceSkin(self.armature.armatureDisplay:getArmature(), partArmatureData:getDefaultSkin());
	-- Remove has been replaced
	table.remove(self.replaceSuitParts, partIndex)
end


--------------------------
-- 触控
-------------------------- 

function M:onTouchBegan(x, y, touches)
	self:randomReplaceSkin()
	--  触控有效
	return true 
end

function M:onTouchMoved(x, y, touches)
	--   
end

function M:onTouchEnded(x, y, touches)
	--  
end



--------------------------
-- 功能函数
--------------------------




--------------------------
-- 属性
--------------------------




--------------------------
-- 父类重写
--------------------------




----------------------------------------------------
-- 析构
--------------------------

function M:onDestructor()
	-- [超类调用]
	M.super.onDestructor(self)
	-- [本类调用]

end








return M






